package com.huazai.juc.bedroom.thread.past;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author Snowflake
 * @date 2022/7/18 0:28
 */
@Slf4j(topic = "compare")
public class SleepAndYield {

    @Test
    public void threadStatusTest() {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t1");
        t1.start();

        log.info("t1 's status is {}", t1.getState());
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        log.info("t1 's status is {}", t1.getState());
    }

    @Test
    public void threadInterruptSleepTest() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.info("enter sleeping...");
//            TimeUnit.SECONDS.sleep(1);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                log.info("wake up...");
                e.printStackTrace();
            }
        });
        t1.setName("t1");
        t1.start();

        Thread.sleep(1000);
        log.info("interrupting...");
        t1.interrupt();
    }
}
